home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 127_01.zip / RAP2.C < prev    next >
Text File  |  1993-06-17  |  10KB  |  407 lines

  1. /*********************************************************************\
  2. ** .---------------------------------------------------------------. **
  3. ** |                                                               | **
  4. ** |                                                               | **
  5. ** |         Copyright (c) 1981, 1982, 1983 by Eric Martz.         | **
  6. ** |                                                               | **
  7. ** |                                                               | **
  8. ** |       Permission is hereby granted to use this source         | **
  9. ** |       code only for non-profit purposes. Publication of       | **
  10. ** |       all or any part of this source code, as well as         | **
  11. ** |       use for business purposes is forbidden without          | **
  12. ** |       written permission of the author and copyright          | **
  13. ** |       holder:                                                 | **
  14. ** |                                                               | **
  15. ** |                          Eric Martz                           | **
  16. ** |                         POWER  TOOLS                          | **
  17. ** |                    48 Hunter's Hill Circle                    | **
  18. ** |                      Amherst MA 01002 USA                     | **
  19. ** |                                                               | **
  20. ** |                                                               | **
  21. ** `---------------------------------------------------------------' **
  22. \*********************************************************************/
  23.  
  24. #include "rap.h"
  25.  
  26. /****************************************************************
  27. handles case of leading blanks or tabs; empty lines 
  28. ******************************************************************/
  29.  
  30. leadbl (line)
  31.     char *line;
  32.     {
  33.     int i;
  34.     char c;
  35. #ifdef DEBUG
  36.     if (Debug) fprintf(STDERR,"Entering leadbl.\n");
  37. #endif
  38.     if (Outwrds) brk(CR);
  39.  
  40.     Tival = Inval; /* literal leading whitespace overrides Pival */
  41.     Skip_cnt = i = 0;
  42.     while((c = line[i]) EQ SPACE OR c EQ TAB) {
  43.         if (c EQ SPACE) Tival += 1;
  44.         if (c EQ TAB) {
  45.             Tival += 1;
  46.             while (Tival - (Tabval * (Tival/Tabval))) Tival += 1;
  47.         }
  48.         i++;
  49.         Skip_cnt++;
  50.     }
  51.     Nextin += Skip_cnt;
  52. #ifdef DEBUG
  53.     if (Debug) fprintf(STDERR,
  54.         "leadbl: Tival=%d, Skip_cnt=%d, Tabval=%d\n",
  55.         Tival, Skip_cnt, Tabval);
  56. #endif
  57. }
  58. /********************************************************************
  59.         produces n empty lines
  60. *********************************************************************/
  61. skip (n)
  62.     int n;
  63.     {
  64.     int i;
  65.     if (Print_at) return(0);
  66. #ifdef DEBUG
  67.     if (Debug) fprintf(STDERR,"SKIP %d line(s)\n", n);
  68. #endif
  69.     for ( i=0; i<n; i++) {
  70.         screen_status();
  71. #ifdef DEBUG
  72.          if (Debug) putchar ('!');
  73. #endif
  74.         putchar (NEWLINE);
  75.     }
  76. }
  77.  
  78.  
  79. /******************************************************************
  80.     indents the proper number of spaces
  81. *******************************************************************/
  82. indent(val)
  83. int val;
  84. {
  85. int i;
  86. if (Print_at) return(0);
  87.  
  88. #ifdef DEBUG
  89. if (Debug) fprintf(STDERR,"INDENT %d spaces(s)\n",val);
  90. #endif
  91.     for ( i=0; i<val; i++ )        putchar( BLANK );
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98. /*******************************************************************
  99.         puts out page header
  100. *******************************************************************/
  101. phead()
  102.     {
  103.     Curpag = Newpag;
  104.     if (Print_at EQ Newpag) Print_at = 0;
  105.     Newpag++;
  106.     Bottom = Plval - M3val - M4val;
  107.     if ( M1val > 0 ) {
  108.         skip ( M1val - 1 );
  109.         puttl ( Header);
  110.     }
  111.     skip ( M2val );
  112.     Lineno = M1val + M2val + 1;
  113. #ifdef DEBUG
  114.     if (Debug) fprintf(STDERR,"After PHEAD, Lineno=%d\n", Lineno);
  115. #endif
  116.     return;
  117. }
  118.  
  119.  
  120. /*********************************************************************
  121.         puts out page footer
  122. *********************************************************************/
  123. pfoot() {
  124.     skip ( M3val );
  125.     if ( M4val > 0 ) {
  126.         puttl ( Footer);
  127.         skip ( M4val - 1 );
  128.      }
  129.     if (Screen_size AND !Print_at)
  130.         fprintf(STDERR,"%s<Bottom of page %d>%s\n", Bar, Curpag, Bar);
  131.     if (!Con_forms AND !Print_at) {
  132.         fprintf(STDERR,"\7");
  133.         wait();
  134.     }
  135.     if (End_wait) {
  136.         fprintf(STDERR,"\7");
  137.         wait();
  138.         End_wait = NO;
  139.     }
  140. }
  141. /*******************************************************************
  142.     put out title line with optional page no.
  143. *******************************************************************/
  144. puttl(title_str)
  145.     char *title_str;
  146.     {
  147.     char buf[MAXLINE], left[MAXLINE], center[MAXLINE], delim_str[2];
  148.     char number[8];
  149.     int space1, len1;
  150.  
  151.     if (Print_at) return(0);
  152.  
  153.     delim_str[0] = Pre_ss_delim;
  154.     delim_str[1] = NULL;
  155.  
  156.     strcpy(buf, title_str); /* SAVE title_str FOR FUTURE USE */
  157.     ss(buf, delim_str); /* PRE SUBSTITUTE CURRENT VALUES */
  158.  
  159.     delim_str[0] = Post_ss_delim;
  160.     sprintf(number, "%d", Curpag);
  161.  
  162. #ifdef DEBUG
  163.     if (Debug) fprintf(STDERR,"Puttl:");
  164. #endif
  165.  
  166.     /* EXTRACT LEFT, CENTER, RIGHT(BUF) */
  167.     substitute(buf, "$F", (Fpin EQ Fp_main? Fn_main: Fn_read));
  168.     center[0] = NULL;
  169.     if (todelim(left, buf, "|") NE ERROR)
  170.         todelim(center,buf,"|");
  171.     else {
  172.         strcpy(left, buf);
  173.         buf[0] = NULL;
  174.     }
  175.     if (*buf) {
  176.         substitute(buf, "#", number);
  177.         if (!*Flush_right)
  178.             pad(buf, 'l', Rmval + (strlen(buf)-truelen(buf)), SPACE);
  179.         else puts(Flush_right);
  180.         ss(buf, delim_str);
  181.         puts(buf);
  182.         putchar('\r');
  183.         if (*Flush_right) restore_mode();
  184.     }
  185.     if (*center) {
  186.         substitute(center, "#", number);
  187.         if (!(*Center_mode)) 
  188.             pad(center, 'l', ((Rmval-truelen(center))/2), SPACE);
  189.         else puts(Center_mode);
  190.         ss(center, delim_str);
  191.         puts(center);
  192.         putchar('\r');
  193.         if (*Center_mode) restore_mode();
  194.     }
  195.     substitute(left, "#", number);
  196.     ss(left, delim_str);
  197.     screen_status();
  198.     puts(left);
  199. #ifdef DEBUG
  200.     if (Debug) fprintf(STDERR,"%s\n",buf);
  201. #endif
  202.     putchar(NEWLINE);    
  203. }
  204. /********************************************************************
  205.     copy title from com_line to ttl
  206. **********************************************************************/
  207. gettl(com_line, ttl)
  208.     char *com_line, *ttl;
  209.     {
  210.     int i;
  211.     char d[2];
  212.  
  213.     #ifdef DEBUG
  214.     if (Debug) fprintf(STDERR,"GETTL command line= <%s>\n", com_line);
  215.     #endif
  216.  
  217.     if (strlen(com_line) < 5) {
  218.         *ttl = NULL;
  219.         return(0);
  220.     }
  221.  
  222.     /* SKIP UNQUOTED LEADING WHITESPACE */
  223.     i=3;
  224.     while(isspace(com_line[i])) i++;
  225.  
  226.     /* STRIP QUOTE IF FOUND */
  227.     if ( com_line[i]  EQ  0x27 /* single quote */
  228.         OR  com_line[i]  EQ  '"')    i++;
  229.  
  230.     strcpy (ttl, &com_line[i]);
  231.  
  232.     #ifdef DEBUG
  233.     if (Debug) fprintf(STDERR,"Title = <%s>\n", ttl);
  234.     #endif
  235. }
  236. /******************************************************************
  237.     space n lines or to bottom of the page
  238. *******************************************************************/
  239. space (n)
  240.     int n;
  241.     {
  242.  
  243. #ifdef DEBUG
  244.     if (Debug) fprintf(STDERR,
  245.         "SPACE %d line(s), Lineno= %d, Outbuf[0]=<decimal %d>\n",
  246.         n, Lineno, Outbuf[0]);
  247. #endif
  248.     brk(CR);    /* flush out last unfilled line */
  249.     if (Lineno > Bottom)    return;    /* end of page */
  250.  
  251.     if ( Lineno  EQ  0 )    /* top of page */
  252.         phead();
  253.  
  254.     skip( min( n, Bottom+1-Lineno ));    /* can't skip past bottom  */
  255.     Lineno = Lineno + n;    /* obvious */
  256.  
  257. #ifdef DEBUG
  258.     if (Debug) fprintf(STDERR,"After spacing, Lineno = %d\n", Lineno);
  259. #endif
  260.     if (Lineno > Bottom) {
  261.         pfoot();    /* print footer if bottom */
  262.         Lineno = 0;
  263.     }
  264.     if (Fill) Tival = Pival;
  265. }
  266.  
  267.  
  268. /*******************************************************
  269.  
  270. *******************************************************/
  271. text (line)
  272.     char *line;
  273.     {
  274.     char wrdbuf [MAXLINE];
  275.     int i, j, k;
  276.     char *p1, *p2;
  277.  
  278.     Nextin = line;
  279. #ifdef DEBUG
  280.     if (Debug) checkbig("TEXT:",line);
  281. #endif
  282.  
  283.     /* USE space() FOR BLANK LINES TO SET Tival TO Pival */
  284.     if (!line[0] OR only(line, "\t ")) {
  285.         space(1);
  286.         goto ul;
  287.     }
  288.     /* IGNORE LEADING @ */
  289.     if (*line EQ '@' AND Ignore_at) {
  290.         line++;
  291.         Nextin++;
  292.     }
  293.  
  294.     /* LEADING WHITESPACE */
  295.     if (Fill AND (line[0]  EQ  BLANK  OR  line[0]  EQ  TAB))
  296.         leadbl (line); /* SETS Skip_cnt */
  297.     else Skip_cnt = 0;
  298.  
  299.     if (Ce_input OR !Fill ) {
  300.         expantab(line+Skip_cnt);
  301.         if (Ce_input) {
  302.             chopwhite('b', line);
  303.             if (!(*Center_mode)) {
  304.                 center (line);
  305.                 Skip_cnt = 0;
  306.             }
  307.         }
  308.         put (line+Skip_cnt,CR);
  309.     }
  310.     else {
  311.         while (getwrd (&Nextin)) {
  312.             getspaces(&Nextin);
  313.             putwrd ();
  314.         }
  315.     }
  316. ul:
  317.     if (Ul_input) Ul_input--;
  318.     if (Ce_input) Ce_input--;
  319.     if (Bo_input) Bo_input--;
  320. #ifdef DEBUG
  321.     if (Debug) checkbig("Text returning.\n","");
  322. #endif
  323. }
  324.  
  325. /***********************************************************
  326.     put out a line of text with correct indentation
  327.     underlining if specified
  328. ************************************************************/
  329. put (line, eol)
  330.     char *line, eol;
  331.     {
  332.     int i, j, k;
  333.  
  334. #ifdef DEBUG
  335.     if (Debug) checkbig("PUT:",line);
  336. #endif
  337.     if (Lineno  EQ  0) phead();
  338.     putline (line,eol);
  339.     skip (min (Lsval-1, Bottom-Lineno));
  340.     Lineno = Lineno + Lsval;
  341. #ifdef DEBUG
  342.     if (Debug) fprintf(STDERR,
  343.         "After PUT: Lineno=%d,  Lsval=%d\n",Lineno, Lsval);
  344. #endif
  345.     if (Lineno > Bottom) {
  346.         pfoot();
  347.         Lineno = 0;
  348.     }
  349. }
  350. /***************************************************
  351. ***************************************************/
  352. restore_mode() {
  353.  
  354. /*    if (Print_at) return(0); */
  355.  
  356.     switch (Justify) {
  357.         case 0:
  358.             puts(Flush_left);
  359.             break;
  360.         case 1:
  361.             puts(Just_one);
  362.             break;
  363.         case 2:
  364.             puts(Just_two);
  365.     }
  366. }
  367. /***************************************************
  368.     TRUE LENGTH
  369. ***************************************************/
  370. truelen(buf)
  371.     char *buf;
  372.     {
  373.     char *p;
  374.     int len, indelim;
  375.  
  376.     len = 0;
  377.     indelim = NO;
  378.  
  379.     /* GET LENGTH OF LINE DISCOUNTING }}, ^H, ETC. */
  380.  
  381.     for (p=buf; *p; p++) {
  382.         if (*p EQ Post_ss_delim) {
  383.             if (!indelim) {
  384.                 indelim = YES;
  385.                 continue;
  386.             }
  387.             else {
  388.                 indelim = NO;
  389.                 if (p > buf AND (*(p-1) EQ Post_ss_delim)) len++;
  390.                 continue;
  391.             }
  392.         }
  393.         if (indelim) continue;
  394.         if (*p EQ BACKSPACE) {
  395.             len--;
  396.             continue;
  397.         }
  398.         len++;
  399.     }
  400.     return(len);
  401. }
  402. /***************************************************
  403.     END OF RAP2.C
  404. ***************************************************/
  405. **********************************
  406. ***************************************************/
  407. restore_mo